/**
 * Created by zzc on 2017/12/15
 */


'use strict'

import axios from 'axios'
import qs from 'qs'
import {BASE_FIND_HOUSE_URL, DIALOG_LOADING, PAGE_LOADING, PageStatus} from "../Constants";
import ProgressHud from "../widget/ProgressHud";
import NetWorkUtil from "../util/NetWorkUtil";
let CancelToken = axios.CancelToken;
let source = CancelToken.source();

let loading=""
let HttpServer = {}

//发出请求的时候的拦截器
axios.interceptors.request.use(config => {
    if(loading === DIALOG_LOADING){
        ProgressHud.show(true,"")
    }
    return config
}, error => {
    return Promise.reject(error)
})

//请求回来之后的拦截器
axios.interceptors.response.use(response => {
    if(loading === DIALOG_LOADING){
        ProgressHud.hide()
    }
    return response
}, error => {
    return Promise.reject(error.response)
})

function errorState(response) {
    //store.commit('UPDATE_LOADING',false)  //隐藏loading
    NetWorkUtil.checkNetworkState((isConnected)=>{
        if(!isConnected){
        }else{
        }
    })
}

function successState(response) {

}

HttpServer.httpServer = (opts, data) => {
    loading = opts.loading
    let Public = { //公共参数
        version: "3.7.3",
        uid: "",
        token: "",
        clientNum: "",
        categoryId: "APP"
    }

    let httpDefaultOpts = { //http默认配置
        method:opts.method,
        url: opts.url,
        timeout: 1000*5,
        params:Object.assign(Public, data),
        cancelToken: source.token,
        data:qs.stringify(Object.assign(Public, data)),
        headers: opts.method ==='get'?{
            'X-Requested-With': 'XMLHttpRequest',
            "Accept": "application/json",
            "Content-Type": "application/json; charset=UTF-8"
        }:{
            'X-Requested-With': 'XMLHttpRequest',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
    }

    if(opts.method ==='get'){
        delete httpDefaultOpts.data
    }else{
        delete httpDefaultOpts.params
    }

    let promise = new Promise(function(resolve, reject) {
        axios(httpDefaultOpts).then((response) => {
                //successState(response)
                console.log("********right******"+response.data)
                if(response.data.code == 0 || response.data.code == 200){
                    resolve(response.data)
                }else {
                    reject(response.data)
                }
            }
        ).catch((response) => {
                //errorState(response)
                console.log("********error******"+response)
                if(axios.isCancel(response)){
                    console.log('Request canceled',response.message);
                }else {
                    //handle error
                    if(typeof(response) =="undefined"){
                        reject("")
                    }else{
                        reject(response.data)
                    }

                }
            }
        )
    })
    return promise
}

HttpServer.cancelRequest= ()=> {
    source.cancel();
}

export default HttpServer